package com.avic.rong.adapter.show;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.activity.ArticleDetailActivity;
import com.avic.rong.activity.VideoDetailActivity;
import com.avic.rong.api.collect.DeleteCollectApi;
import com.avic.rong.api.collect.ZanCancelApi;
import com.avic.rong.bean.show_bean.EsInfoBean;
import com.avic.rong.bean.show_bean.TypeBean;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by TSJ-2018-0492 on 2019/8/13.
 */

public class TransverseAdapter extends RecyclerView.Adapter<TransverseAdapter.ViewHolder> {

    private Context context;
    private List<TypeBean> data;
    private List<EsInfoBean> esBean;

    private String logo;
    private DeleteCollectApi deleteCollectApi;
    private ZanCancelApi zanCancelApi;


    public TransverseAdapter(Context context, List<TypeBean> data, List<EsInfoBean> esBean) {
        this.context = context;
        this.data = data;
        this.esBean = esBean;
        deleteCollectApi = new DeleteCollectApi((MainActivity) context, deleteCollectListener);
        zanCancelApi = new ZanCancelApi((MainActivity) context, httpOnNextListener);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.type_8_layout, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

        if (data != null) {
            TypeBean typeBean = data.get(i);
            viewHolder.tvRongrongFollow.setText(typeBean.getAuthor());
            if (!TextUtils.isEmpty(typeBean.getPublicTime()))
                viewHolder.tvTimeFollow.setText(typeBean.getPublicTime().split(" ")[0]);
            if (esBean != null) {
                //        String coverImgOne = typeBean.getCoverImgOne();
                String coverImgOne = typeBean.getContImg();
                if (coverImgOne != null) {
                    viewHolder.imageview.setVisibility(View.VISIBLE);
                    Glide.with(context).load(coverImgOne).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(viewHolder.imageview);
                } else {
                    viewHolder.imageview.setVisibility(View.GONE);
                }

                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (5 == typeBean.getTypeFlg()) {
                            Intent intent = new Intent(context, VideoDetailActivity.class);
                            intent.putExtra("contId", typeBean.getContId());
                            context.startActivity(intent);
                        } else {
                            Intent intent = new Intent(context, ArticleDetailActivity.class);
                            intent.putExtra("contId", typeBean.getContId());
                            context.startActivity(intent);
                        }
                    }
                });

                viewHolder.tvDianzansize.setText(typeBean.getThumb() + "");
                if (typeBean.getIsV() != 1) {
                    viewHolder.type_8_daV.setVisibility(View.GONE);
                }
                viewHolder.tvShoucangsize.setText(typeBean.getFavouritesNum() + "");
                viewHolder.tvPinglunsize.setText(typeBean.getAppraiseNum() + "");
//                Glide.with(context).load(logo)
//                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
//                        .error(R.mipmap.deafult_image_header)
//                        .into(viewHolder.ivIconFollow);
                viewHolder.tvNeirongFollow.setText(typeBean.getTitle());
                viewHolder.tv_sign.setText(typeBean.getAuthenInfo());

                viewHolder.ivDianzan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (SharedPreferencesUtil.isLogin()) {
                            for (int k = 0; k < esBean.size(); k++) {
                                EsInfoBean esInfoBean = esBean.get(k);
                                int cont_id = esBean.get(k).getCont_id();
                                int contId = data.get(i).getContId();
                                if (cont_id == contId) {
                                    int isThumb = esInfoBean.getIsThumb();
                                    if (isThumb != 1) {
                                        esInfoBean.setIsThumb(1);
                                        Glide.with(context).load(R.mipmap.orangezan2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(viewHolder.ivDianzan);
                                        int num = typeBean.getThumb() + 1;
                                        typeBean.setThumb(num);
                                        ToastTools.showPublicToast("点赞成功");
                                        String logintoken = SharedPreferencesUtil.getInstance(context).getLogintoken();
                                        zanCancelApi.setArgument(contId, 1, logintoken);
                                        HttpManager.getInstance().doHttpDeal(zanCancelApi);
                                        notifyDataSetChanged();
                                        return;
                                    } else {
                                        esInfoBean.setIsThumb(0);
                                        Glide.with(context).load(R.mipmap.whitezan2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(viewHolder.ivDianzan);
                                        int num = typeBean.getThumb() - 1;
                                        typeBean.setThumb(num);
                                        ToastTools.showPublicToast("已取消点赞");
                                        String logintoken = SharedPreferencesUtil.getInstance(context).getLogintoken();
                                        zanCancelApi.setArgument(contId, 1, logintoken);
                                        HttpManager.getInstance().doHttpDeal(zanCancelApi);
                                        notifyDataSetChanged();
                                        return;
                                    }

                                }
                            }


                        } else {
                            ToastTools.showToast("请先登录");

                        }

                    }
                });

                viewHolder.ivShoucang.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (SharedPreferencesUtil.isLogin()) {

                            for (int k = 0; k < esBean.size(); k++) {
                                EsInfoBean esInfoBean = esBean.get(k);
                                int cont_id = esBean.get(k).getCont_id();
                                int contId = data.get(i).getContId();
                                if (cont_id == contId) {
                                    int isFavorites = esInfoBean.getIsFavorites();
                                    if (isFavorites != 1) {
                                        esInfoBean.setIsFavorites(1);
                                        Glide.with(context).load(R.mipmap.yellowstar2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(viewHolder.ivShoucang);
                                        int num = typeBean.getFavouritesNum();
                                        num++;
                                        typeBean.setFavouritesNum(num);
                                        ToastTools.showPublicToast("收藏成功");
                                        String logintoken = SharedPreferencesUtil.getInstance(context).getLogintoken();
                                        deleteCollectApi.setArgument(logintoken, 12, contId);
                                        HttpManager.getInstance().doHttpDeal(deleteCollectApi);
                                        notifyDataSetChanged();
                                        return;
                                    } else {
                                        esInfoBean.setIsFavorites(0);
                                        Glide.with(context).load(R.mipmap.whitestar2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(viewHolder.ivShoucang);
                                        int num = typeBean.getFavouritesNum();
                                        num--;
                                        typeBean.setFavouritesNum(num);
                                        ToastTools.showPublicToast("已取消收藏");
                                        String logintoken = SharedPreferencesUtil.getInstance(context).getLogintoken();
                                        deleteCollectApi.setArgument(logintoken, 12, contId);
                                        HttpManager.getInstance().doHttpDeal(deleteCollectApi);
                                        notifyDataSetChanged();
                                        return;
                                    }

                                }
                            }

                        } else {
                            ToastTools.showToast("请先登录");

                        }
                    }
                });

                viewHolder.ivIconFollow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, PersonalHomeActivity.class);
                        intent.putExtra("homeUserID", typeBean.getAdminId() + "");
                        context.startActivity(intent);
                    }
                });

                for (int k = 0; k < esBean.size(); k++) {
                    EsInfoBean esInfoBean = esBean.get(k);
                    int cont_id = esBean.get(k).getCont_id();
                    int contId = data.get(i).getContId();
                    if (cont_id == contId) {
                        int isFavorites = esInfoBean.getIsFavorites();
                        int isThumb = esInfoBean.getIsThumb();
                        int view_num = esInfoBean.getView_num();
                        logo = esInfoBean.getLogo();

                        Glide.with(context).load(esInfoBean.getLogo())
                                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                                .error(R.mipmap.deafult_image_header)
                                .into(viewHolder.ivIconFollow);
                        if (isThumb == 1) {
                            Glide.with(context).load(R.mipmap.orangezan2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(viewHolder.ivDianzan);
                        } else {
                            Glide.with(context).load(R.mipmap.whitezan2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(viewHolder.ivDianzan);
                        }


                        if (isFavorites == 1) {
                            Glide.with(context).load(R.mipmap.yellowstar2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(viewHolder.ivShoucang);
                        } else {
                            Glide.with(context).load(R.mipmap.whitestar2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(viewHolder.ivShoucang);
                        }
                        return;
                    }
                }

            }
        }

    }

    HttpOnNextListener<BaseResultEntity> deleteCollectListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
//                if (isFavorites) {
//                    ToastTools.showPublicToast("收藏成功");
//                } else {
//                    ToastTools.showPublicToast("已取消收藏");
//                }
            } else {
                ToastTools.showPublicToast("收藏失败");
            }
        }
    };

    HttpOnNextListener<BaseResultEntity> httpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals("200")) {
//                Toast.makeText(context,"操作成功",Toast.LENGTH_LONG).show();
            } else {
//                Toast.makeText(context,"操作失败",Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    public int getItemCount() {
        return data.size();
    }


    static

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_icon_follow)
        ImageView ivIconFollow;
        @BindView(R.id.tv_rongrong_follow)
        TextView tvRongrongFollow;
        @BindView(R.id.tv_time_follow)
        TextView tvTimeFollow;
        @BindView(R.id.tv_neirong_follow)
        TextView tvNeirongFollow;
        @BindView(R.id.iv_shoucang)
        ImageView ivShoucang;
        @BindView(R.id.tv_shoucangsize)
        TextView tvShoucangsize;
        @BindView(R.id.iv_dianzan)
        ImageView ivDianzan;
        @BindView(R.id.tv_dianzansize)
        TextView tvDianzansize;
        @BindView(R.id.iv_pinglun)
        ImageView ivPinglun;
        @BindView(R.id.tv_pinglunsize)
        TextView tvPinglunsize;
        @BindView(R.id.tv_sign)
        TextView tv_sign;
        private ImageView imageview;
        private final ImageView type_8_daV;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            type_8_daV = view.findViewById(R.id.type_8_daV);
            imageview = view.findViewById(R.id.one_imageview);

        }
    }


}
